package cn.baddonkey.superquery.service;

import cn.baddonkey.superquery.dao.ModuleBaseInfoMapper;
import cn.baddonkey.superquery.dao.ModuleQueryMapper;
import cn.baddonkey.superquery.dao.ModuleQueryReturnFieldMapper;
import cn.baddonkey.superquery.dao.ModuleQuerySelectFieldMapper;
import cn.baddonkey.superquery.exception.ServiceException;
import cn.baddonkey.superquery.model.ModuleBaseInfo;
import cn.baddonkey.superquery.model.ModuleQuery;
import cn.baddonkey.superquery.model.ModuleQueryReturnField;
import cn.baddonkey.superquery.model.ModuleQuerySelectField;
import cn.baddonkey.superquery.utils.BeanUtils;
import cn.baddonkey.superquery.vo.ModuleQueryConfigVo;
import cn.baddonkey.superquery.vo.ModuleQueryReturnFieldVo;
import cn.baddonkey.superquery.vo.ModuleQuerySelectFieldVo;
import cn.baddonkey.superquery.vo.ModuleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ModuleQueryConfigService {

    @Autowired
    private ModuleBaseInfoMapper moduleBaseInfoMapper;

    @Autowired
    private ModuleQueryMapper moduleQueryMapper;

    @Autowired
    private ModuleQueryReturnFieldMapper returnFieldMapper;

    @Autowired
    private ModuleQuerySelectFieldMapper selectFieldMapper;

    public ModuleQueryConfigVo detail(Long id, Long versionId) {
        ModuleBaseInfo moduleBaseInfo = moduleBaseInfoMapper.selectById(id);
        if (moduleBaseInfo == null) {
            throw new ServiceException("模块不存在");
        }
        ModuleQueryConfigVo transform = BeanUtils.transform(moduleBaseInfo, ModuleQueryConfigVo.class);
        ModuleQuery moduleQuery = moduleQueryMapper.selectOne(Wrappers.<ModuleQuery>lambdaQuery()
                .eq(ModuleQuery::getModuleCode, moduleBaseInfo.getModuleCode())
                .eq(ModuleQuery::getIsDeleted, 0)
                .eq(versionId != null, ModuleQuery::getId, versionId)
                .orderByDesc(ModuleQuery::getCreateTime).last("limit 1"));
        if (moduleQuery != null) {
            ModuleQueryVo moduleQueryVo = BeanUtils.transform(moduleQuery, ModuleQueryVo.class);
            transform.setModuleQueryVo(moduleQueryVo);
            List<ModuleQueryReturnField> returnFieldList = returnFieldMapper.selectList(Wrappers.<ModuleQueryReturnField>lambdaQuery()
                    .eq(ModuleQueryReturnField::getModuleQueryId, moduleQueryVo.getId())
                    .eq(ModuleQueryReturnField::getIsDeleted, 0));
            List<ModuleQuerySelectField> selectFieldList = selectFieldMapper.selectList(Wrappers.<ModuleQuerySelectField>lambdaQuery()
                    .eq(ModuleQuerySelectField::getModuleQueryId, moduleQueryVo.getId())
                    .eq(ModuleQuerySelectField::getIsDeleted, 0));
            transform.setReturnFieldVoList(BeanUtils.batchTransform(returnFieldList, ModuleQueryReturnFieldVo.class));
            transform.setSelectFieldVoList(BeanUtils.batchTransform(selectFieldList, ModuleQuerySelectFieldVo.class));
        }
        return transform;
    }

    @Transactional(rollbackFor = Exception.class)
    public void add(ModuleQueryConfigVo vo) {
        ModuleQuery moduleQuery = BeanUtils.transform(vo.getModuleQueryVo(), ModuleQuery.class);
        moduleQuery.setCreateTime(null);
        moduleQuery.setId(null);
        moduleQueryMapper.insert(moduleQuery);
        List<ModuleQueryReturnField> returnFieldList = BeanUtils.batchTransform(vo.getReturnFieldVoList(), ModuleQueryReturnField.class);
        returnFieldList.forEach(x -> {
            x.setModuleQueryId(moduleQuery.getId());
        });
        List<ModuleQuerySelectField> selectFieldList = BeanUtils.batchTransform(vo.getSelectFieldVoList(), ModuleQuerySelectField.class);
        selectFieldList.forEach(x -> {
            x.setModuleQueryId(moduleQuery.getId());
        });
        selectFieldMapper.selectAllByFieldDesc("1");
        returnFieldMapper.saveBatch(returnFieldList);
        selectFieldMapper.saveBatch(selectFieldList);
    }

    public Page<ModuleQueryVo> listVersion(Long id, Integer pageNum, Integer pageSize) {
        ModuleBaseInfo moduleBaseInfo = moduleBaseInfoMapper.selectById(id);
        if (moduleBaseInfo == null) {
            throw new ServiceException("模块不存在");
        }
        LambdaQueryWrapper<ModuleQuery> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ModuleQuery::getModuleCode, moduleBaseInfo.getModuleCode());
        lambdaQueryWrapper.eq(ModuleQuery::getIsDeleted, 0);
        lambdaQueryWrapper.orderByDesc(ModuleQuery::getCreateTime);
        Page<ModuleQuery> moduleQueryPage = moduleQueryMapper.selectPage(new Page<>(pageNum, pageSize), lambdaQueryWrapper);
        return BeanUtils.copyPage(moduleQueryPage, ModuleQueryVo.class);
    }
}
